<?php

namespace App\Repositories;

use App\Models\TestingDeveloper;
use Cache;

class TestingDeveloperRepository extends Repository
{
    static $tag = 'testing.developer';


    public function getById($id)
    {
        $cacheKey = getCacheKey('testing.developer.info') . $id;
        $value = $this->remember($cacheKey, function () use ($id) {
            return TestingDeveloper::where('id', $id)->first();
        });
        return $value;
    }


    public function store($data)
    {
        $this->clearCache($this->tag());
        $res = TestingDeveloper::firstOrCreate($data);
        return $res;
    }


    public function getDeveloperOptions()
    {
        $list = TestingDeveloper::all();
        $data = [];
        if ($list) {
            $obj = array();
            foreach ($list as $key => $val) {
                $obj['id'] = $val['union_code'];
                $obj['text'] = $val['name'];
                array_push($data, $obj);
            }
        }
        return $data;
    }

    public function checkUnionCode($data)
    {
        $cacheKey = getCacheKey('testing.developer.info') . $data['union_code'];
        $value = $this->remember($cacheKey, function () use ($data) {
            return TestingDeveloper::select('union_code')->where($data)->first();
        });
        return $value;
    }

    public function getByUnionCode($unionCode){
        $cacheKey = getCacheKey('testing.developer.info.by.') . $unionCode;
        $value = $this->remember($cacheKey, function () use ($unionCode) {
            return TestingDeveloper::where('union_code', $unionCode)->first();
        });
        return $value;
    }


    public function getByAppid($appid, $appsecret)
    {
        $cacheKey = getCacheKey('testing.developer.info') . $appid . $appsecret;
        $value = $this->remember($cacheKey, function () use ($appid, $appsecret) {
            return TestingDeveloper::where('appid', $appid)
                ->where('appsecret', $appsecret)
                ->first();
        });
        return $value;
    }


    /**
     * 根据别名获取开发者信息
     */
    public function getByAbbreviation($abbreviation){
        $cacheKey = getCacheKey('testing.developer.info') . $abbreviation;
        $value = $this->remember($cacheKey, function () use ($abbreviation) {
            return TestingDeveloper::where('abbreviation', $abbreviation)->first();
        });
        return $value;
    }

    /**
     * 只通过appid来获取
     */
    public function getWithAppid($appid){
        $cacheKey = getCacheKey('testing.cache_key.developer.info') . $appid ;
        $value = $this->remember($cacheKey, function () use ($appid) {
            return TestingDeveloper::where('appid', $appid)
                ->first();
        });
        return $value;
    }

    public function getTestingDeveloperItemByAbbreviation($abbreviation){
        $cacheKey = getCacheKey('testing.cache_key.developer.info') . $abbreviation;
        $value = $this->remember($cacheKey, function () use ($abbreviation) {
            return TestingDeveloper::where('abbreviation', $abbreviation)
                ->first();
        });
        return $value;
    }



    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }


    public function model()
    {
        return app(TestingDeveloper::class);
    }

    public function tag()
    {
        return self::$tag;
    }
}